package two.tree;

/**
 * 翻转一棵二叉树
 * 时间复杂度：O(n)
 * 思路：递归（边界、本层逻辑、子问题、还原现场）
 */
public class InvertTree {
    public TreeNode invertTree(TreeNode root) {
        //边界
        if(root == null) return null;
        //子问题
        invertTree(root.left);
        invertTree(root.right);
        //本层逻辑
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        return root;
    }
}
class TreeNode{
    int val;
    TreeNode left;
    TreeNode right;
    public TreeNode(){}
    public TreeNode(int val){
        this.val = val;
    }
    public TreeNode(int val, TreeNode left, TreeNode right){
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
